package com.afkt.project.ui.adapter;

import com.afkt.project.model.item.ButtonValue;
import com.afkt.project.slice.DevEnvironmentLibSlice;
import com.afkt.project.slice.DevWidgetUISlice;
import com.afkt.project.slice.FrameworkSlice;
import com.afkt.project.slice.UiSlice;
import com.afkt.project.ui.slice.ViewAssistActivity;
import com.afkt.project.util.LogUtils;
import com.afkt.project.util.SkipUtils;
import com.dev.utils.app.toast.ToastTintUtils;
import com.ohos.afkt.ResourceTable;
import ohos.agp.components.*;
import ohos.app.Context;

import java.util.List;

public class MainAdapter extends BaseItemProvider implements Component.ClickedListener {

    private static final String TAG = "ButtonAdapter";

    private List<ButtonValue> myList;

    private Context context;

    public MainAdapter(List<ButtonValue> myList, Context context) {
        this.myList = myList;
        this.context = context;
    }

    @Override
    public int getCount() {

        LogUtils.error(TAG, "count = " + myList.size());
        return myList.size();
    }

    @Override
    public Object getItem(int i) {
        return myList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public Component getComponent(int i, Component component, ComponentContainer componentContainer) {

        ViewHolder viewHolder = null;
        if (component == null) {
            LogUtils.error(TAG, "i = " + i);
            component = LayoutScatter.getInstance(context).parse(ResourceTable.Layout_adapter_item_ui, null, false);
            viewHolder = new ViewHolder();
            viewHolder.mBtn = (Button) component.findComponentById(ResourceTable.Id_list_button);
            component.setTag(viewHolder);
        }

        viewHolder = (ViewHolder) component.getTag();
        viewHolder.mBtn.setTag(i);
        viewHolder.mBtn.setText(myList.get(i).text);
        viewHolder.mBtn.setClickedListener(this);
        return component;
    }

    @Override
    public void onClick(Component component) {
        switch (component.getId()) {
            case ResourceTable.Id_list_button:
                int b = (int) component.getTag();
                ButtonValue buttonValue = myList.get(b);
                switch (buttonValue.type) {
                    case ButtonValue.MODULE_FRAMEWORK:
                        SkipUtils.startActivity(new FrameworkSlice(), buttonValue);
                        break;
                    case ButtonValue.MODULE_LIB:
                        break;
                    case ButtonValue.MODULE_UI:
                        SkipUtils.startActivity(new UiSlice(), buttonValue);
                        break;
                    case ButtonValue.MODULE_OTHER:
                        break;
                    case ButtonValue.MODULE_DEV_WIDGET:
                        SkipUtils.startActivity(new DevWidgetUISlice(), buttonValue);
                        break;
                    case ButtonValue.MODULE_DEV_ENVIRONMENT:
                        SkipUtils.startActivity(new DevEnvironmentLibSlice(), buttonValue);
                        break;
                    default:
                        ToastTintUtils.warning("未处理 " + buttonValue.text + " 事件");
                        break;
                }
        }
    }


    static class ViewHolder {
        Button mBtn;

        public ViewHolder() {
        }
    }
}
